<script>
import commonListFactory from '../components/CommonList'
import { termination, freezeAndThawTemplate } from '@/api/channelTask2/change'
import { listUserRoles } from '@/api/common'
import { getToken } from '@/utils/auth'
const archiveDepartmentRoleCode = '1687163581945'
const customerManagerRoleCode = '1687163452792'
const setting = [
  {
    beforeProp: 'type',
    prop: 'reportType',
    label: '提报类型',
    minWidth: 120,
    propEnum: {
      domainCode: 'marketing',
      appCode: 'slm',
      dictTypeCode: 'CTM_REPORT_TYPE'
    }
  },
  {
    beforeProp: 'schedule',
    prop: 'preCloseTime',
    label: '预计关闭时间',
    minWidth: 140
  },
  {
    beforeProp: 'schedule',
    prop: 'isTempUnfreeze',
    label: '是否临时解冻',
    minWidth: 130,
    propEnum: {
      domainCode: 'marketing',
      appCode: 'slm',
      dictTypeCode: 'BOOLEANS'
    }
  }
]
export default {
  name: 'FreezeAndthaw',
  data() {
    return {
      tabsConfig: [
        {
          name: 'all',
          label: '全部',
          defaultFiltersRaw: [
            { id: 'type', property: 'type', value: '[E,F]', operator: 'in' }
          ],
          defaultParamsRaw: {},
          tableConfig: {
            interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
            tabIndex: 1,
            buttonGroupCode: 'all',
            // 显示顺序：优先显示变更进度值为“已保存”的数据，
            // 后显示变更进度为“已提交”的数据，相同进度的数据按照创建时间倒序排列
            sortersRow: [
              { 'id': 'schedule', 'property': 'schedule', 'direction': 'ASC' },
              { 'id': 'createTime', 'property': 'createTime', 'direction': 'DESC' }
            ],
            setting,
            btnList: process.env.NODE_ENV === 'development' ? [
              { buttonName: '冻结申请', optCode: 'freeze_apply', outerFlag: true },
              { buttonName: '解冻申请', optCode: 'unfreeze_apply', outerFlag: true },
              { buttonName: '冻结', optCode: 'freeze', outerFlag: true },
              { buttonName: '解冻', optCode: 'unfreeze', outerFlag: true },
              { buttonName: '冻结导入', optCode: 'freeze_import', outerFlag: true },
              { buttonName: '冻结导入模板', optCode: 'freeze_temlpate', outerFlag: true },
              { buttonName: '解冻导入', optCode: 'unfreeze_import', outerFlag: true },
              { buttonName: '解冻导入模板', optCode: 'unfreeze_temlpate', outerFlag: true }
            ] : null
          }
        },
        {
          name: 'save',
          label: '已保存',
          defaultFiltersRaw: [
            { id: 'type', property: 'type', value: '[E,F]', operator: 'in' },
            { id: 'schedule', property: 'schedule', value: 'A', operator: '=' }
          ],
          defaultParamsRaw: {},
          tableConfig: {
            interfacePath: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerChange/page`,
            tabIndex: 2,
            buttonGroupCode: 'save',
            sortersRow: [
              { 'id': 'createTime', 'property': 'createTime', 'direction': 'DESC' }
            ],
            setting,
            btnList: process.env.NODE_ENV === 'development' ? [
              { buttonName: '编辑', optCode: 'edit', outerFlag: true },
              { buttonName: '终止', optCode: 'termination', outerFlag: true }
            ] : null
          }
        }
      ],
      importFreezeVisible: false,
      importThawVisible: false,
      listUserRolesData: []
    }
  },
  computed: {
    fileFreezeImportConfig() {
      return [
        {
          code: 'CtmCustomerFreezeExcelDTO',
          name: '冻结导入',
          async: false,
          headers: { token: getToken() },
          apiUrl: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerFreeze/importFreeze`
        }
      ]
    },
    fileThawImportConfig() {
      return [
        {
          code: 'CtmCustomerUnfreezeExcelDTO',
          name: '解冻导入',
          async: false,
          headers: { token: getToken() },
          apiUrl: `${process.env.VUE_APP_BASE_API}/api/v1/ctmCustomerFreeze/importUnfreeze`
        }
      ]
    },
    isArchiveDepartment() {
      return this.listUserRolesData.some(e => e.roleCode === archiveDepartmentRoleCode)
    },
    isCustomerManager() {
      return this.listUserRolesData.some(e => e.roleCode === customerManagerRoleCode)
    }
  },
  created() {
    this.getListUserRoles()
  },
  methods: {
    getListUserRoles() {
      listUserRoles().then(res => {
        this.listUserRolesData = res.data
      })
    },
    toDetail(row) {
      // 根据是否冻结判断
      let src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/freezeDetail.vue'
      if (row.type === 'F') {
        src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/thawDetail.vue'
      }
      this.$pageStack.push(src, {
        props: {
          id: row.id
        }
      })
    },
    handleBtnOperate(flag, rows) {
      this[flag] && this[flag](rows)
    },
    freeze_apply(rows) {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/freezeApply.vue', null, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    unfreeze_apply(rows) {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/thawApply.vue', null, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    freeze() {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/freezeArchive.vue', null, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    freeze_import(rows) {
      this.importFreezeVisible = true
    },
    freeze_temlpate() {
      freezeAndThawTemplate('CtmCustomerFreezeExcelDTO', '冻结导入模板.xlsx').then(() => {
        this.$message.success('模板下载成功!')
      })
    },
    unfreeze_import(rows) {
      this.importThawVisible = true
    },
    unfreeze_temlpate() {
      freezeAndThawTemplate('CtmCustomerUnfreezeExcelDTO', '解冻导入模板.xlsx').then(() => {
        this.$message.success('模板下载成功!')
      })
    },
    unfreeze() {
      this.$pageStack.push('src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/thawArchive.vue', null, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    edit(rows) {
      if (!this.singleJudge(rows, '编辑', true)) {
        return
      }
      const row = rows[0]
      let src = ''
      if (row.reportType === 'A') {
        if (!this.isCustomerManager) {
          this.$message.warning('您不能编辑该条数据!')
          return
        }
        src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/freezeApply.vue'
        if (row.type === 'F') {
          src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/thawApply.vue'
        }
      } else {
        if (!this.isArchiveDepartment) {
          this.$message.warning('您不能编辑该条数据!')
          return
        }
        src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/freezeArchive.vue'
        if (row.type === 'F') {
          src = 'src/views/channelTask2/dealerMarketingInfo/archive/freezeAndthaw/components/thawArchive.vue'
        }
      }
      this.$pageStack.push(src, {
        props: {
          id: rows[0].id
        }
      }, flag => {
        flag && this.$refs.commonList.refreshTableBoxData(flag)
      })
    },
    termination(rows) {
      if (!this.singleJudge(rows, '终止')) {
        return
      }
      this.$msgbox({
        title: '终止',
        message: '提交后将终止此变更数据，是否确认？',
        showCancelButton: true,
        showConfirmButton: true,
        type: 'warning'
      }).then(() => {
        return termination(rows[0].id).then(res => {
          this.$message.success('操作成功!')
          this.$refs.commonList.refreshTableBoxData('onPage')
        })
      })
    }
  },
  render(h) {
    return h(commonListFactory({ tabsConfig: this.tabsConfig, searchConfig: [
      {
        type: 'select',
        prop: 'reportType',
        label: '提报类型',
        value: '',
        placeholder: '',
        config: {
          remote: false,
          paramField: 'keyWord', // 远程搜索参数名
          remoteUrl: `/data-dict/api/v2/dictionary/CTM_REPORT_TYPE`,
          defaultProps: {
            label: 'dictName',
            value: 'dictCode'
          },
          paramsRaw: { appCode: 'slm', domainCode: 'marketing' },
          apiHeader: {
            // 'Permission-Code': 'per_pur_order_middle_page',
            // 'Permission-App': 'slm'
          }
        }
      },
      {
        type: 'select',
        prop: 'isTempUnfreeze',
        label: '是否临时解冻',
        value: '',
        placeholder: '',
        config: {
          remote: true,
          // paramField: 'keyWord',// 远程搜索参数名
          remoteUrl: `/data-dict/api/v2/dictionary/BOOLEANS`,
          defaultProps: {
            label: 'dictName',
            value: 'dictCode'
          },
          paramsRaw: { appCode: 'slm', domainCode: 'marketing' },
          apiHeader: {
          // 'Permission-Code': 'per_pur_order_middle_page',
          // 'Permission-App': 'slm'
          }
        }
      },
      {
        type: 'date',
        prop: 'preCloseTime',
        label: '预计关闭时间',
        value: '',
        placeholder: '',
        dateType: 'datetimerange',
        dateConfig: {
          format: 'yyyy-MM-dd',
          valueFormat: 'yyyy-MM-dd'
        }
      }
    ], changeType: 'E F' }), {
      on: {
        btnOperate: this.handleBtnOperate,
        'on-detail': this.toDetail
      },
      ref: 'commonList',
      scopedSlots: {
        default: (props) => {
          return h('div', [
            h('import-files', {
              props: {
                title: '冻结导入',
                visible: this.importFreezeVisible,
                config: this.fileFreezeImportConfig
              },
              on: {
                'update:visible': (v) => {
                  this.importFreezeVisible = v
                }
              }
            }),
            h('import-files', {
              props: {
                title: '解冻导入',
                visible: this.importThawVisible,
                config: this.fileThawImportConfig
              },
              on: {
                'update:visible': (v) => {
                  this.importThawVisible = v
                }
              }
            })
          ])
        }
      }
    })
  }
}
</script>
